﻿using MXComponent.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;

namespace MXComponent.Views.charview
{
    /// <summary>
    /// materialChar.xaml 的交互逻辑
    /// </summary>
    public partial class formulaReport : Page
    {
        formuladetailhistoryDB fdhistoryDb = new formuladetailhistoryDB();
        public formulaReport()
        {
            InitializeComponent();
        }
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.startDate.Text))
            {
                MessageBox.Show("开始日期不能为空！");
                this.startDate.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(this.endDate.Text))
            {
                MessageBox.Show("结束日期不能为空！");
                this.endDate.Focus();
                return;
            }
            DataSet ds = fdhistoryDb.GetformulaReport(Convert.ToDateTime( this.startDate.Text).ToString("yyyy-MM-dd"), Convert.ToDateTime(this.endDate.Text).ToString("yyyy-MM-dd"));
            this.dgvList.ItemsSource = ds.Tables[0].DefaultView;
        }

     
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.startDate.Text = DateTime.Today.ToLongDateString();
            this.endDate.Text = DateTime.Today.ToLongDateString();
            this.dgvList.LoadingRow += new EventHandler<DataGridRowEventArgs>(this.dataGridEquipment_LoadingRow);
        }
        private void dataGridEquipment_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void dgvList_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
        {
            DataGrid MainDataGrid = sender as DataGrid;
            var cell = MainDataGrid.CurrentCell;
            DataRowView item = cell.Item as DataRowView;
            string formulahistoryid = item["formulahistoryid"].ToString();
            DataSet ds = fdhistoryDb.Getformuladetailhistory(formulahistoryid);
            DataGrid DetailsDataGrid = e.DetailsElement as DataGrid;
            DetailsDataGrid.ItemsSource = ds.Tables[0].DefaultView;
        }
        private void Expander_Expanded(object sender, RoutedEventArgs e)
        {
            //this.m_mainVM.DetailExpanded(this.grid1.SelectedIndex);

            this.dgvList.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
        }

        private void Expander_Collapsed(object sender, RoutedEventArgs e)
        {
            //this.m_mainVM.DetailCollapsed(this.grid1.SelectedIndex);
            this.dgvList.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Collapsed;
        }
        public static void Export(DataGrid dataGrid, string _fileName)
        {
            DataTable dt = new DataTable();
            for (int i = 0; i < dataGrid.Columns.Count; i++)
            {
                if (dataGrid.Columns[i].Visibility == System.Windows.Visibility.Visible )//只导出可见列
                {
                    dt.Columns.Add(dataGrid.Columns[i].Header.ToString());//构建表头

                }
            }

            for (int i = 0; i < dataGrid.Items.Count; i++)
            {
                int columnsIndex = 0;
                DataRow row = dt.NewRow();
                for (int j = 0; j < dataGrid.Columns.Count; j++)
                {
                    if (dataGrid.Columns[j].Visibility == System.Windows.Visibility.Visible )
                    {
                        if (columnsIndex == 0)
                        {
                            row[columnsIndex] = (i+1).ToString();
                        }
                        else if (dataGrid.Items[i] != null && (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock) != null)//填充可见列数据
                        {
                            row[columnsIndex] = (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock).Text.ToString();
                        }
                        else
                        {
                            row[columnsIndex] = "";
                        }
                        columnsIndex++;
                    }
                }
                dt.Rows.Add(row);
            }


            System.Windows.Forms.SaveFileDialog save = new System.Windows.Forms.SaveFileDialog();
            //save.Filter = "Excel文件(*.xls)|";
            save.Title = "请选择要保存的路径";
            save.FileName = _fileName;

            if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = save.FileName;
               //ExportFiles.ExportExcel(fileName, dt);
                ExcelHelper.DataTableToExcel(fileName, dt, "MySheet", true);
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            Export(this.dgvList,"配方报表"+DateTime.Now.ToString("yyyyMMddHHmmss")+ ".xls");
        }
    }
}
